package com.fanli.scheduler.controller;

import com.fanli.scheduler.bean.HiveEsTemplate;
import com.fanli.scheduler.bean.Result;
import com.fanli.scheduler.entity.EtlMetaColumn;
import com.fanli.scheduler.entity.EtlMetaTableBase;
import com.fanli.scheduler.entity.EtlMetaTableBaseExample;
import com.fanli.scheduler.mapping.EtlMetaTableBaseMapper;
import com.fanli.scheduler.service.HiveMetaService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xu.liu on 2016/9/5.
 */
@RequestMapping("/meta")
@Controller
public class HiveMetaController {
    @Autowired
    HiveMetaService metaService;

    @RequestMapping(value = "/basetable",method = RequestMethod.GET)
    @ResponseBody
    public Result getMetaTableBases(@RequestParam("db") String db){
        List<EtlMetaTableBase> bases = metaService.getEnableMetaTableBasesByDb(db);
        System.err.println(bases);
        Result result=new Result();
        if(CollectionUtils.isNotEmpty(bases)){
            result.setIsSuccess(true);
            result.setResults(bases);
        }else{
            result.setIsSuccess(false);
        }
        return result;
    }

    @RequestMapping(value = "/queryColumnByTid",method = RequestMethod.GET)
    @ResponseBody
    public Result queryColumnByTid(@RequestParam("tid") String tid){
        List<EtlMetaColumn> columns = metaService.getAllEnableByTid(tid);
        System.err.println(columns);
        Result result=new Result();
        if(CollectionUtils.isNotEmpty(columns)){
            List<HiveEsTemplate> templates=new ArrayList<HiveEsTemplate>();
            for (EtlMetaColumn column : columns) {
                HiveEsTemplate t=new  HiveEsTemplate();
                t.setColumnName(column.getColumnName());
                t.setPartition(column.getIsPartition()==1?true:false);
                t.setColumnType(column.getColumnType());
                t.setTid(tid);
                templates.add(t);
            }
            result.setIsSuccess(true);
            result.setResults(templates);
        }else{
            result.setIsSuccess(false);
        }
        return result;
    }
}
